package com.ltsznh.android.heartrate;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.SurfaceHolder;

/**
 * Created by ltsznh on 14-11-24.
 */
class RateViewThread extends Thread {

    private final SurfaceHolder surfaceHolder;
    private Paint paint = new Paint();
    private Paint clearPaint = new Paint();

    private Canvas canvas;
    private int[] values;
    private int count = 0;

    private int width;
    private int height;

    private int zeroValue;
    private int max;
    private int min;

    private int bl = 10;

    private long startMs;
    private long endMs;

    private int rate;

    private int[] rateMaxs;
    private int rateMaxCount;


    public RateViewThread(SurfaceHolder surfaceHolder) {
        // TODO Auto-generated constructor stub
        this.surfaceHolder = surfaceHolder;

        paint.setColor(Color.rgb(0, 153, 204));
        paint.setTextSize(60);
//        clearPaint.setColor(Color.rgb(0,0,0));
        paint.setAntiAlias(true);//锯齿效果
//        paint.
    }

    public void setSize(int width, int height) {
        this.width = width;
        this.height = height;
        values = new int[width / bl];
        rateMaxs = new int[width / bl];
    }

    public void run() {
        canvas = surfaceHolder.lockCanvas();

        try {
            synchronized (surfaceHolder) {
                draw();//画
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if (canvas != null) {
                surfaceHolder.unlockCanvasAndPost(canvas);
            }
        }

    }

    public boolean setValue(int value) {
        if (count >= width / bl) {
            count = 0;
            rateMaxCount = 0;
            return true;
        }
        values[count] = value;
        int fff = (count - 400) > 0 ? (count - 400) : 0;
        if (count == 0) {
            max = value;
            min = value;
//            rateMax = value;
        }
        int lmax = values[fff];
        int lmin = values[fff];

//        rateAvg = 0;
        for (int i = fff; i < count; i++) {
            if (values[count] > max) max = values[count];
            if (values[count] < min) min = values[count];
//            rateAvg = rateAvg + values[count];
        }
        if (count > 3) {
            int cc = values[count] - values[count - 1];
            if (((float) cc / (max - min)) < -0.3) {
                rateMaxs[rateMaxCount] = count;
                if (rateMaxCount > 0) {
                    endMs = System.currentTimeMillis();
                    rate = (int) (60 / (float) ((float) (endMs - startMs) / 1000 / (rateMaxCount)));
                    MainActivity.etTime.setText("" + (float) ((float) (endMs - startMs) / 1000));
                } else {
                    startMs = System.currentTimeMillis();
                }
                MainActivity.etRate.setText("" + rate);
                MainActivity.etRateCount.setText("" + rateMaxCount);
                MainActivity.npAdjust.setValue(rateMaxCount);
                MainActivity.npAdjust.setMaxValue(rateMaxCount + 10);

                rateMaxCount++;
            }
        }


        count++;
        return false;
    }

    public void draw() {
        canvas.drawRect(0, 0, width, height, clearPaint);//清空
        for (int i = 1; i < count; i++) {
            canvas.drawLine((i - 1) * bl, ((float) (values[i - 1] - min) / (max - min)) * height, i * bl, ((float) (values[i] - min) / (max - min)) * height, paint);
//            canvas.drawCircle(i * bl,((float)(values[i] - min) / (max - min)) * height,3,paint);
            canvas.drawText(rate + "", 0, 80, paint);
        }
        if (rateMaxCount > 0) {
            canvas.drawLine(rateMaxs[0] * bl, 0, rateMaxs[0] * bl, height, paint);
            canvas.drawLine(rateMaxs[rateMaxCount - 1] * bl, 0, rateMaxs[rateMaxCount - 1] * bl, height, paint);
        }
        for (int i = 0; i < rateMaxCount; i++) {
            canvas.drawCircle(rateMaxs[i] * bl, ((float) (values[rateMaxs[i]] - min) / (max - min)) * height, 3, paint);
        }
    }

    public void reset() {
        count = 0;
        rateMaxCount = 0;
        rate = 0;
    }
}